package com.inossem.warehousestorage_inossem.request;

import android.content.Context;

import com.inossem.utils.exception.InossemException;
import com.inossem.utils.request.VolleyUtils;
import com.inossem.warehousestorage_inossem.bean.common.Bean;
import com.inossem.warehousestorage_inossem.bean.common.BeanNew;
import com.inossem.warehousestorage_inossem.bean.common.HeadBean;
import com.inossem.warehousestorage_inossem.constant.RequestConstant;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;

/**
 * Created by leij on 2017/6/13.
 */

public class Request {

    private static final String[] FAILING_CODE_LIST = {RequestConstant.ERROR_CODE_SAP};

    protected static void excuteJsonBean(Context context, Bean bean, String code, String msg, GsonBeanListener gsonBeanListener) {
        if (RequestConstant.CODE_SUCCESS.equals(code)) {
            //递归检索排序jsonBean中所有list集合中包含指定字段的list
//            gsonBeanListener.onSuccess(BeanUtils.sortBean(bean));
            //未排序的jsonBean
            gsonBeanListener.onSuccess(bean);
        } else {
            if (gsonBeanListener.useDefaultError()) {
                gsonBeanListener.onDefaultError(context, code, msg);
            } else {
                gsonBeanListener.onError(context, code, msg);
            }
        }
    }

    protected static void requestException(String url) {
        throw new InossemException("request send fail!, request is null, url:" + url);
    }

    protected static void excuteJsonBeanNew(Context context, BeanNew bean, GsonBeanListener gsonBeanListener) {
        HeadBean head = bean.getHead();
        if (RequestConstant.STATUS_SUCCESS.equals(head.getStatus())) {
            //递归检索排序jsonBean中所有list集合中包含指定字段的list
//            gsonBeanListener.onSuccess(BeanUtils.sortBean(bean));
            //未排序的jsonBean
            gsonBeanListener.onSuccess(bean);
        } else if (isFailing(head)) {
            gsonBeanListener.onFailing(bean);
        } else {
            if (gsonBeanListener.useDefaultError()) {
                gsonBeanListener.onDefaultError(context, head.getError_code(), head.getMsg());
            } else {
                gsonBeanListener.onError(context, head.getError_code(), head.getMsg());
            }
        }
    }

    private static boolean isFailing(HeadBean head) {
        if (RequestConstant.STATUS_ERROR.equals(head.getStatus())) {
            for (int i = 0; i < FAILING_CODE_LIST.length; i++) {
                if (FAILING_CODE_LIST[i].equals(head.getError_code())) {
                    return true;
                }
            }
            return false;
        } else {
            return false;
        }
    }

    protected static String addRequest(Context context, com.android.volley.Request<?> request, String url) {
        String tag = null;
        if (request != null) {
            tag = (String) request.getTag();
            VolleyUtils.addRequest(context, request);
        } else {
            requestException(url);
        }
        return tag;
    }

}
